home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
General
/
PTR-TCL v2.1
/
2) TCL Munger
/
Munge.c
< prev
next >
Wrap
Text File
|
1994-02-17
|
5KB
|
224 lines
/*
* munge.c
*/
extern void Idle ( void ) ;
extern short vRefNum ;
void MungeFolder ( long parID ) ;
extern void Message ( unsigned char * ) ;
extern Boolean DoLine ( unsigned char * line ) ;
CInfoPBRec rec ;
short err ;
Str63 name ;
Str255 str ;
short lineNo ;
static void
CopyP ( unsigned char * from , unsigned char * to ) {
BlockMove ( from , to , 1 + * from ) ;
}
static void
ConcP ( unsigned char * to , unsigned char * tail ) {
short len = * tail ;
if ( len + * to > 255 ) {
len = 255 - * to ;
}
BlockMove ( tail + 1 , to + * to + 1 , len ) ;
* to += len ;
}
static void
ErrMsg ( unsigned char * why , unsigned char * file , short code ) {
Str15 nn ;
long tick ;
static long lastBeep = 0 ;
NumToString ( code , nn ) ;
CopyP ( "\p ERROR " , str ) ;
ConcP ( str , why ) ;
ConcP ( str , "\p " ) ;
ConcP ( str , nn ) ;
ConcP ( str , "\p\r" ) ;
Message ( str ) ;
tick = TickCount ( ) + 30 ;
if ( lastBeep < tick - 600 ) { /* Only beep every 10 seconds if lots of errors */
SysBeep ( 20 ) ;
lastBeep = tick ;
}
do {
Idle ( ) ;
} while ( TickCount ( ) < tick ) ;
}
static long
GetLine ( Handle text , long pos , unsigned char * into ) {
long end = GetHandleSize ( text ) ;
lineNo ++ ;
* into = 0 ;
while ( pos < end ) {
if ( * into == 255 ) {
ErrMsg ( "\pLong Line!" , "\pLine" , lineNo ) ;
return pos ;
}
( * into ) ++ ;
into [ * into ] = ( * text ) [ pos ] ;
pos ++ ;
if ( into [ * into ] == '\r' ) {
return pos ;
}
}
return pos ;
}
static long
PutLine ( Handle text , long pos , long len , unsigned char * str ) {
ConcP ( str , "\p// " ) ; /* Comment out old line */
Munger ( text , pos , NULL , 0L , str + 1 , * str ) ;
return pos + len + * str ;
}
/* This function contains the total text of a TEXT file. */
/* The contents of the handle will be re-written to disk when done. */
/* Call Idle ( ) now and then for lengthy tasks */
static void
MungeHandle ( Handle text ) {
Str255 line ;
long curLine = 0 ;
long lineLen ;
int ix = 20 ;
lineNo = 0 ;
while ( curLine < GetHandleSize ( text ) ) {
ix -- ;
if ( ! ix ) {
Idle ( ) ;
ix = 20 ;
}
lineLen = GetLine ( text , curLine , str ) - curLine ;
if ( lineLen != str [ 0 ] ) {
// Message ( "\plineLen in MungeHandle wrong" ) ; /* Should be equal! */
}
if ( DoLine ( str ) ) {
curLine = PutLine ( text , curLine , lineLen , str ) ;
} else {
curLine += lineLen ;
}
}
}
static void
MungeFile ( short vRef , long parID , unsigned char * fn ) {
Handle text ;
short ref = 0 ;
long size ;
Message ( fn ) ;
err = HOpenDF ( vRef , parID , fn , fsRdWrPerm , & ref ) ;
if ( err ) {
ErrMsg ( "\pOpening" , fn , err ) ;
return ;
}
err = GetEOF ( ref , & size ) ;
if ( ! err ) {
err = SetFPos ( ref , fsFromStart , 0L ) ;
}
if ( err ) {
ErrMsg ( "\pStarting" , fn , err ) ;
FSClose ( ref ) ;
return ;
}
text = NewHandle ( size ) ;
if ( ! text ) {
ErrMsg ( "\pAllocating" , fn , MemError ( ) ) ;
FSClose ( ref ) ;
return ;
}
HLock ( text ) ;
err = FSRead ( ref , & size , * text ) ;
if ( err ) {
ErrMsg ( "\pReading" , fn , err ) ;
FSClose ( ref ) ;
DisposeHandle ( text ) ;
}
HUnlock ( text ) ;
MungeHandle ( text ) ;
size = GetHandleSize ( text ) ;
err = SetEOF ( ref , size ) ;
if ( ! err ) {
err = SetFPos ( ref , fsFromStart , 0L ) ;
}
if ( err ) {
ErrMsg ( "\pFinishing" , fn , err ) ;
FSClose ( ref ) ;
DisposeHandle ( text ) ;
return ;
}
HLock ( text ) ;
err = FSWrite ( ref , & size , * text ) ;
if ( err ) {
ErrMsg ( "\pWriting" , fn , err ) ;
FSClose ( ref ) ;
DisposeHandle ( text ) ;
return ;
}
err = FSClose ( ref ) ;
if ( ! err ) {
err = FlushVol ( NULL , vRef ) ;
}
DisposeHandle ( text ) ;
if ( err ) {
ErrMsg ( "\pClosing" , fn , err ) ;
}
Message ( "\p OK\r" ) ;
}
void
MungeFolder ( long parID ) {
short ix ;
for ( ix = 1 ; ix > 0 ; ix ++ ) {
rec . hFileInfo . ioCompletion = 0L ;
rec . hFileInfo . ioFVersNum = 0 ;
rec . hFileInfo . ioNamePtr = name ;
rec . hFileInfo . ioVRefNum = vRefNum ;
rec . hFileInfo . ioDirID = parID ;
rec . hFileInfo . ioFDirIndex = ix ;
err = PBGetCatInfoAsync ( & rec ) ;
do {
Idle ( ) ;
} while ( rec . hFileInfo . ioResult == 1 ) ;
err = rec . hFileInfo . ioResult ;
if ( err ) {
break ;
}
if ( rec . hFileInfo . ioFlAttrib & 0x10 ) { /* Folder */
CopyP ( "\p\r•Entering Folder " , str ) ;
ConcP ( str , name ) ;
ConcP ( str , "\p\r" ) ;
Message ( str ) ;
MungeFolder ( rec . hFileInfo . ioDirID ) ;
Message ( "\p---- Leaving folder ----\r\r" ) ;
} else if ( rec . hFileInfo . ioFlFndrInfo . fdType == 'TEXT' ) {
MungeFile ( vRefNum , parID , name ) ;
}
}
}